import org.apache.poi.sl.usermodel.*;
import org.apache.poi.xslf.usermodel.*;
import java.awt.*;
import java.io.*;

public class PPTGenerator {
    public static void main(String[] args) {
        try (XMLSlideShow ppt = new XMLSlideShow()) {
            // 添加幻灯片
            XSLFSlide slide = ppt.createSlide();

            // 设置背景图片
            String imagePath = "path/to/your/background.jpg";
            try (FileInputStream fis = new FileInputStream(imagePath)) {
                XSLFPictureData pictureData = ppt.addPicture(fis, PictureData.PictureType.JPEG);
                XSLFPictureShape pictureShape = slide.createPicture(pictureData);
                // 设置图片填充整个幻灯片
                Dimension pgsize = ppt.getPageSize();
                pictureShape.setAnchor(new Rectangle(0, 0, pgsize.width, pgsize.height));
            } catch (FileNotFoundException e) {
                System.err.println("背景图片文件未找到，请检查路径: " + e.getMessage());
            }

            // 创建文本框
            XSLFTextBox textBox = slide.createTextBox();
            // 设置文本框位置和大小
            textBox.setAnchor(new Rectangle(100, 200, 400, 200));

            // 创建段落
            XSLFTextParagraph paragraph = textBox.addNewTextParagraph();

            // 创建文本运行
            XSLFTextRun textRun = paragraph.addNewTextRun();
            textRun.setText("我爱中国");
            textRun.setFontFamily("宋体");
            textRun.setFontSize(18d);
            textRun.setBold(true);
            textRun.setFontColor(Color.YELLOW);

            // 保存PPT文件
            try (FileOutputStream out = new FileOutputStream("output.pptx")) {
                ppt.write(out);
                System.out.println("PPT生成成功！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
